﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BO.Admin;
using BO.CompanyUserBO;

namespace ProjectQuaDep.Control
{
    public partial class SearchLeft : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
                //hang san xuat
                AddBinCompany();
                //
                AddZoneProduct();
        }
        protected void AddZoneProduct()
        {
            DataTable dtListAll = ProductsBO.getAllZoneListBO();
            DataTable dtListFilterTo = new DataTable();
            DataTable dtListFilterNho = new DataTable();
            DataTable dtShow = new DataTable();
            dtListFilterTo.Columns.Add(BO.Const.tbZoneProduct.ZoneId);
            dtListFilterTo.Columns.Add(BO.Const.tbZoneProduct.title);
            dtListFilterTo.Columns.Add(BO.Const.tbZoneProduct.ParentId);
            dtListFilterNho.Columns.Add(BO.Const.tbZoneProduct.ZoneId);
            dtListFilterNho.Columns.Add(BO.Const.tbZoneProduct.title);
            dtListFilterNho.Columns.Add(BO.Const.tbZoneProduct.ParentId);
            dtShow.Columns.Add(BO.Const.tbZoneProduct.ZoneId);
            dtShow.Columns.Add(BO.Const.tbZoneProduct.title);
            dtShow.Columns.Add(BO.Const.tbZoneProduct.ParentId);
            //DataRow drFirst = dtShow.NewRow();
            //drFirst[BO.Const.tbZoneProduct.ZoneId] = "0";
            //drFirst[BO.Const.tbZoneProduct.title] = "Tất cả";
            //drFirst[BO.Const.tbZoneProduct.ParentId] = "0";
            //dtShow.Rows.Add(drFirst);
            //chia ra de add sau
            if (dtListAll.Rows.Count > 0)
            {
                for (int i = 0; i < dtListAll.Rows.Count; i++)
                {
                    if (int.Parse(dtListAll.Rows[i][BO.Const.tbZoneProduct.ParentId].ToString()) > 0)
                    {
                        //cat nho
                        DataRow dr = dtListFilterNho.NewRow();
                        dr[BO.Const.tbZoneProduct.ZoneId] = dtListAll.Rows[i][BO.Const.tbZoneProduct.ZoneId].ToString();
                        dr[BO.Const.tbZoneProduct.title] = dtListAll.Rows[i][BO.Const.tbZoneProduct.title].ToString();
                        dr[BO.Const.tbZoneProduct.ParentId] = dtListAll.Rows[i][BO.Const.tbZoneProduct.ParentId].ToString();
                        dtListFilterNho.Rows.Add(dr);
                    }
                    else
                    {
                        //cat to
                        DataRow dr = dtListFilterTo.NewRow();
                        dr[BO.Const.tbZoneProduct.ZoneId] = dtListAll.Rows[i][BO.Const.tbZoneProduct.ZoneId].ToString();
                        dr[BO.Const.tbZoneProduct.title] = dtListAll.Rows[i][BO.Const.tbZoneProduct.title].ToString();
                        dr[BO.Const.tbZoneProduct.ParentId] = dtListAll.Rows[i][BO.Const.tbZoneProduct.ParentId].ToString();
                        dtListFilterTo.Rows.Add(dr);
                    }
                }
            }
            //add vao
            if (dtListFilterTo.Rows.Count > 0)
            {
                for (int i = 0; i < dtListFilterTo.Rows.Count; i++)
                {
                    //add cat to sau do add luon cat con vao
                    DataRow dr = dtShow.NewRow();
                    int ParentId = int.Parse(dtListFilterTo.Rows[i][BO.Const.tbZoneProduct.ParentId].ToString());
                    int ID = int.Parse(dtListFilterTo.Rows[i][BO.Const.tbZoneProduct.ZoneId].ToString());
                    dr[BO.Const.tbZoneProduct.ZoneId] = ID.ToString();
                    dr[BO.Const.tbZoneProduct.title] = dtListFilterTo.Rows[i][BO.Const.tbZoneProduct.title].ToString();
                    dr[BO.Const.tbZoneProduct.ParentId] = ParentId;
                    dtShow.Rows.Add(dr);
                    for (int j = 0; j < dtListFilterNho.Rows.Count; j++)
                    {
                        int ParentId2 = int.Parse(dtListFilterNho.Rows[j][BO.Const.tbZoneProduct.ParentId].ToString());
                        if (ParentId2 == ID)
                        {
                            DataRow dr2 = dtShow.NewRow();
                            dr2[BO.Const.tbZoneProduct.ZoneId] = dtListFilterNho.Rows[j][BO.Const.tbZoneProduct.ZoneId].ToString();
                            dr2[BO.Const.tbZoneProduct.title] = "---" + dtListFilterNho.Rows[j][BO.Const.tbZoneProduct.title].ToString();
                            dr2[BO.Const.tbZoneProduct.ParentId] = dtListFilterNho.Rows[j][BO.Const.tbZoneProduct.ParentId].ToString();
                            dtShow.Rows.Add(dr2);
                        }
                    }
                }
            }

            for (int i = 0; i < dtShow.Rows.Count; i++)
            {
                ltZoneProduct.Text += "<option value=\"" + dtShow.Rows[i][BO.Const.tbZoneProduct.ZoneId].ToString() + "\">" + dtShow.Rows[i][BO.Const.tbZoneProduct.title].ToString() + "</option>";
            }
            //ddlProduct.DataSource = dtShow;
            //ddlProduct.DataTextField = BO.Const.tbZoneProduct.title;
            //ddlProduct.DataValueField = BO.Const.tbZoneProduct.ZoneId;
            //ddlProduct.DataBind();
        }
        protected void AddBinCompany()
        {
            DataTable dtCp = CompanyUBO.GetAllCompanyByStatusBO("1");
            for (int i = 0; i < dtCp.Rows.Count; i++)
            {
                ltCompany.Text += "<option value=\"" + dtCp.Rows[i][BO.Const.tbCompanies.CompanyID].ToString() + "\">" + dtCp.Rows[i][BO.Const.tbCompanies.NameCp].ToString() + "</option>";
            }
            //ddlCompany.DataSource = dtCp;
            //ddlCompany.DataTextField = BO.Const.tbCompanies.NameCp;
            //ddlCompany.DataValueField = BO.Const.tbCompanies.CompanyID;
            //ddlCompany.DataBind();
        }
        protected void cmdSearch_Click(object sender, EventArgs e)
        {
            //string search = txtSearch.Text.Trim();
            //string CompanyID = ddlCompany.SelectedValue.ToString();
            //string zoneid = ddlProduct.SelectedValue.ToString();
            //Response.Write("<script>alert('" + CompanyID + "')</script>");
            //Response.Redirect("/tim-kiem-san-pham.htm?PageIndex=1&ZoneID=" + zoneid + "&KeySearch=" + search + "&CompanyID=" + CompanyID);
        }
    }
}